package com.xing.common;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * Created by Bradish7Y on 2017/9/1.
 * 后台取国际化的信息
 */
@Component
public class MessageManager {
    private static MessageSource messageSource;

    public static String getMessage(String key) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(key, null, locale);
    }

    /**
     * @param key
     * @param arg 填充 {1} {2}
     * @return
     */
    public static String getMessage(String key, String... arg) {
        Locale locale = LocaleContextHolder.getLocale();
        Object[] args = new Object[arg.length];
        for (int i = 0; i < arg.length; i++) {
            args[i] = arg[i];
        }
        return messageSource.getMessage(key, args, locale);
    }

    @Resource
    public void setMessageSource(MessageSource messageSource) {
        MessageManager.messageSource = messageSource;
    }
}
